<?php

 /**
  * @package JooS
  * @subpackage CodeGenerator
  */

  require_once "JooS/CodeGenerator/Abstract.php";

  class JooS_CodeGenerator_Property extends JooS_CodeGenerator_Abstract {
    /**
     * @var bool
     */
    private $_static = false;

    /**
     * @var string
     */
    private $_visibility;

    /**
     * @var mixed
     */
    private $_value = null;

    /**
     * @param string $name
     */
    protected function __construct($name) {
      parent::__construct($name);

      $this->setVisibility(self::VISIBILITY_PUBLIC);
      $this->setValue(null);
      $this->setStatic(false);
    }

    /**
     * @return string
     */
    public function compile() {
      $PHPCode = array();

      $PHPCode[] = $this->getVisibility();
      if ($this->isStatic())
        $PHPCode[] = "static";

      $PHPCode[] = "\$" . $this->getName();
      $PHPCode[] = "=";
      $PHPCode[] = var_export($this->getValue(), true);

      return implode(" ", $PHPCode) . ";";
    }

    /**
     * @param string $name
     * @return JooS_CodeGenerator_Property
     */
    public static function newInstance($name) {
      return new self($name);
    }

    /**
     * @param bool $static
     * @return JooS_CodeGenerator_Property
     */
    public function setStatic($static) {
      $this->_static = !!$static;
      return $this;
    }

    /**
     * @return bool
     */
    public function isStatic() {
      return $this->_static;
    }

    /**
     * @param string $visibility
     * @return JooS_CodeGenerator_Property
     */
    public function setVisibility($visibility) {
      switch ($visibility) {
        case self::VISIBILITY_PUBLIC:
        case self::VISIBILITY_PROTECTED:
        case self::VISIBILITY_PRIVATE:
          $this->_visibility = $visibility;
          break;
        default:
          $this->_visibility = self::VISIBILITY_PUBLIC;
          break;
      }
      return $this;
    }

    /**
     * @return string
     */
    public function getVisibility() {
      return $this->_visibility;
    }

    /**
     * @param mixed $value
     * @return JooS_CodeGenerator_Property
     */
    public function setValue($value) {
      if (is_null($value) || is_scalar($value) || is_array($value))
        $this->_value = $value;
      else
        trigger_error("Type mismatch", E_USER_NOTICE);
        
      return $this;
    }

    /**
     * @return mixed
     */
    public function getValue() {
      return $this->_value;
    }

    /**
     * @return JooS_CodeGenerator_Property
     */
    public function unsetValue() {
      $this->_value = null;
      return $this;
    }
  }
